// 将数字字符串转换为纯数字
function toPureNumber(value) {
  if (typeof value !== 'string') {
    return value;
  }

  value = value.trim();

  value = value.replace(/\+/g, ''); // Remove commas

  // Handle "亿" suffix
  if (/^\$?[\d.]+亿$/.test(value)) {
    const numStr = value.replace(/[^\d.]/g, '');
    const num = parseFloat(numStr) * 100000000;
    return num;
  }

  // Handle "万" suffix
  if (/^\$?[\d.]+万(\+)?$/.test(value)) {
    const numStr = value.replace(/[^\d.]/g, '');
    const num = parseFloat(numStr) * 10000;
    return num;
  }

  // Handle pure numbers
  if (/^\$?[\d.]+$/.test(value)) {
    const numStr = value.replace(/[^\d.]/g, '');
    const num = parseFloat(numStr);
    return num;
  }

  // Handle "%" suffix
  if (/^[\d.]+%$/.test(value)) {
    const numStr = value.replace(/[^\d.]/g, '');
    const num = parseFloat(numStr) / 100;
    return num;
  }

  // Return as-is for other formats
  return value;
}

// 提取单个节点的文本内容
function extractTextFromNode(node) {
  if (!node) return null;
  return node.textContent.trim();
}

// 提取节点的文本内容并转换为数字
function extractNumberFromNode(node) {
  if (!node) return null;
  return toPureNumber(node.textContent);
}

// 从XPath获取单个节点
function getNodeByXPath(xpath, contextNode = document) {
  return document.evaluate(
    xpath,
    contextNode,
    null,
    XPathResult.FIRST_ORDERED_NODE_TYPE,
    null
  ).singleNodeValue;
}

// 从XPath获取多个节点
function getNodesByXPath(xpath, contextNode = document) {
  return document.evaluate(
    xpath,
    contextNode,
    null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    null
  );
}

// 从节点中提取所有文本内容
function getAllTextContent(node) {
  if (!node) return '';
  
  const textNodes = document.evaluate(
    './/text()',
    node,
    null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    null
  );
  
  let text = '';
  for (let i = 0; i < textNodes.snapshotLength; i++) {
    if (text !== '')
      text += ' ' // Add space between words (if any)
    text += textNodes.snapshotItem(i).nodeValue.trim();
  }
  
  return text;
}

// 从键值对容器中提取数据
function extractKeyValuePairs(containerXPath, keyXPath, valueXPath) {
  const result = {};
  const containers = getNodesByXPath(containerXPath);
  
  for (let i = 0; i < containers.snapshotLength; i++) {
    const container = containers.snapshotItem(i);
    const keyNode = getNodeByXPath(keyXPath, container);
    const valueNode = getNodeByXPath(valueXPath, container);
    
    if (keyNode && valueNode) {
      result[extractTextFromNode(keyNode)] = toPureNumber(extractTextFromNode(valueNode));
    }
  }
  
  return result;
}

// 从表格中提取数据
function extractTableData(headerXPath, rowXPath) {
  // 提取表头
  const headerNodes = getNodesByXPath(headerXPath);
  const keys = [];
  
  for (let i = 0; i < headerNodes.snapshotLength; i++) {
    const text = getAllTextContent(headerNodes.snapshotItem(i));
    if (text) {
      keys.push(text);
    }
  }
  
  // 提取行数据
  const rowNodes = getNodesByXPath(rowXPath);
  const result = [];
  
  for (let i = 0; i < rowNodes.snapshotLength; i++) {
    const row = rowNodes.snapshotItem(i);
    const cellNodes = getNodesByXPath('./td', row);
    
    const rowData = {};
    for (let j = 0; j < cellNodes.snapshotLength && j < keys.length; j++) {
      const cell = cellNodes.snapshotItem(j);
      const text = getAllTextContent(cell);
      
      if (keys[j]) {
        rowData[keys[j]] = toPureNumber(text);
      }
    }
    
    result.push(rowData);
  }
  
  return result;
}

function getXPathAttributeValue(xpath) {
    const result = document.evaluate(
        xpath,
        document,
        null,
        XPathResult.STRING_TYPE,
        null
    );
    return result.stringValue;
}


// 导出所有工具函数
export {
  toPureNumber,
  extractTextFromNode,
  extractNumberFromNode,
  getNodeByXPath,
  getNodesByXPath,
  getAllTextContent,
  extractKeyValuePairs,
  extractTableData,
  getXPathAttributeValue
};
